<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生表</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/jquery.js"></script>
    <script src="/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
    <h2>学生信息表</h2>
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
        新增学生
    </button>
    <form id="searchForm">
        <div class="form-group">
            <label for="searchField">选择查询条件</label>
            <select class="form-control" id="searchField">
                <option value="name">姓名</option>
                <option value="sex">性别</option>
            </select>
        </div>
        <div class="form-group">
            <label for="searchValue">输入查询值</label>
            <input type="text" class="form-control" id="searchValue" placeholder="输入查询值">
        </div>
        <button type="button" class="btn btn-primary" onclick="searchStudent();">查询</button>
    </form>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>学号</th>
            <th>性别</th>
            <th>成绩</th>
            <th style="width: 100px">操作</th>
        </tr>
        </thead>
        <tbody id="studentTb">
        </tbody>
    </table>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">学生信息</h4>
            </div>
            <div class="modal-body">
                <form id="formStudent">
                    <input type="hidden" id="id" name="id"/>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="姓名">
                    </div>
                    <div class="form-group">
                        <label for="no">学号</label>
                        <input type="text" class="form-control" id="no" name="no" placeholder="学号">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="1">男</option>
                            <option value="2">女</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="score">成绩</label>
                        <input type="text" class="form-control" id="score" name="score" placeholder="成绩">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="updateStudent();">更改</button>
                <button type="button" class="btn btn-primary" onclick="addStudent();">新增</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    function addStudent() {
        var student = {
            name: $("#name").val(),
            no: $("#no").val(),
            sex: $("#sex").val(),
            score: $("#score").val()
        };

        $.ajax({
            url: "/webapi/student/insert",
            method: "post",
            contentType: "application/json",
            data: JSON.stringify(student)
        }).done(function () {
            loadTable();
            $("#myModal").modal("hide");
        });
    }

    function editStudent(id) {
        $.ajax({
            url: "/webapi/student/get/" + id,
            method: "get"
        }).done(function (student) {
            $("#id").val(student.id);
            $("#name").val(student.name);
            $("#no").val(student.no);
            $("#sex").val(student.sex);
            $("#score").val(student.score);
            $("#myModal").modal("show");
        });
    }

    function updateStudent() {
        var student = {
            id: $("#id").val(),
            name: $("#name").val(),
            no: $("#no").val(),
            sex: $("#sex").val(),
            score: $("#score").val()
        };

        $.ajax({
            url: "/webapi/student/update",
            method: "put",
            contentType: "application/json",
            data: JSON.stringify(student)
        }).done(function () {
            loadTable();
            $("#myModal").modal("hide");
        });
    }

    function deleteStudent(id) {
        if (confirm("确定要删除该学生信息吗？")) {
            $.ajax({
                url: "/webapi/student/delete/" + id,
                method: "delete"
            }).done(function () {
                loadTable();
            });
        }
    }

    function searchStudent() {
        var field = $("#searchField").val();
        var value = $("#searchValue").val();
        var queryParams = {};
        queryParams[field] = value;

        $.ajax({
            url: "/webapi/student/search",
            method: "get",
            data: queryParams
        }).done(function(rs) {
            var len = rs.length;
            var html = "";
            for (var i = 0; i < len; i++) {
                var item = rs[i];
                html += "<tr>"
                    + "<td>" + item.id + "</td>"
                    + "<td>" + item.name + "</td>"
                    + "<td>" + item.no + "</td>"
                    + "<td>" + item.sex + "</td>"
                    + "<td>" + item.score + "</td>"
                    + "<td> <a href='#' onclick='editStudent(" + item.id + ");'>编辑</a> | <a href='#' onclick='deleteStudent(" + item.id + ");'>删除</a></td>"
                    + "</tr>";
            }
            $("#studentTb").html(html);
        }).fail(function(jqXHR, textStatus, errorThrown) {
            console.log("Request failed: " + textStatus + ", " + errorThrown);
        });
    }


    function loadTable() {
        $.ajax({
            url: "/webapi/student/list",
            method: "get"
        }).done(function (rs) {
            renderTable(rs);
        });
    }

    function renderTable(data) {
        var html = "";
        data.forEach(function (item) {
            html += "<tr>"
                + "<td>" + item.id + "</td>"
                + "<td>" + item.name + "</td>"
                + "<td>" + item.no + "</td>"
                + "<td>" + (item.sex === 1 ? "男" : "女") + "</td>"
                + "<td>" + item.score + "</td>"
                + "<td><a href='#' onclick='editStudent(" + item.id + ");'>编辑</a> | <a href='#' onclick='deleteStudent(" + item.id + ");'>删除</a></td>"
                + "</tr>";
        });
        $("#studentTb").html(html);
    }

    $(function () {
        loadTable();
    });
</script>
</body>
</html>
